﻿using EasyCore.UnifyResult.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.UnifyResult
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 增加API方法、过淲器、管道未处理的异常操作
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddApiExceptionHandler(this IServiceCollection services)
        {
            //在执行方法上增加工作单元执行逻辑
            services.Configure<MvcOptions>(options =>
            {
                options.Filters.Add<ApiExceptionFilter>();
            });
            return services;
        }

        /// <summary>
        /// 增加统一结果处理过淲器
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddUnifyResult(this IServiceCollection services)
        {
            services.Configure<MvcOptions>(options =>
            {
                options.Filters.Add<UnifyResultFilter>();
            });
            return services;
        }

        /// <summary>
        /// 添加全局异常处理中间件
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static WebApplication UseGlobalExceptionHandler(this WebApplication app)
        {
            app.UseMiddleware<ExceptionHandlerMiddleware>();
            return app;
        }


    }
}
